package net.qunzhi.modules.historyspider.client;


import net.qunzhi.common.utils.DateUtils;
import net.qunzhi.modules.cms.entity.TbFlowStateEntity;
import net.qunzhi.modules.cms.entity.TbPressureStateEntity;
import net.qunzhi.modules.cms.service.SpiderService;
import net.qunzhi.modules.cms.utils.DiaoduUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;

@Component
public class xiParse {

    @Autowired private SpiderService spiderService;
    public static xiParse xiParse;
    @PostConstruct
    public void init() {
        xiParse = this;
    }

    public static void getData(String entity, Integer equipmentId, String maxtime, String pressureOrFlow){

        //EquipmentPressureStateService equipmentPressureStateService = new EquipmentPressureStateService();
        Document doc = Jsoup.parse(entity);
        final Elements table = doc.select("table");
        if(!"".equals(table.toString())){

            Elements trs = table.get(4).select("tr");
//            SpiderService spiderService = new SpiderService();
            if("pressure".equals(pressureOrFlow)) {
                ArrayList<TbPressureStateEntity> list = new ArrayList<>();
                for (int i = 0; i < trs.size(); i++) {
                    Elements tds = trs.get(i).select("td");
                    ArrayList<String> arr = new ArrayList<>();

                    for (int j = 0; j < tds.size(); j++) {

                        String text = tds.get(j).text();
                        arr.add(text);
                        //System.out.println(text);
                    }

                    if (!"".equals(arr.get(0)) && !"".equals(arr.get(1)) && !"".equals(arr.get(2))) {
                        //如果数据库最大时间比爬取到的时间小才进行插入操作
                        if (maxtime.compareTo(arr.get(0)) < 0) {
                            TbPressureStateEntity pressureStateEntity = new TbPressureStateEntity();
                            pressureStateEntity.setTime(LocalDateTime.parse(arr.get(0), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))
                                    .setValue(arr.get(1))
                                    .setState(arr.get(2))
                                    .setEquipmentId(equipmentId);
                            list.add(pressureStateEntity);
                        }

                    }
                }
                if(list.size()>0){
                    // 批量插入压力数据
                    xiParse.spiderService.insertPressureState(list);
                }
            }else {
//                EquipmentFlowStateService equipmentFlowStateService = new EquipmentFlowStateService();
                ArrayList<TbFlowStateEntity> list = new ArrayList<>();

                for(int i = 0;i<trs.size();i++){
                    Elements tds = trs.get(i).select("td");
                    ArrayList<String> arr = new ArrayList<>();

                    for(int j = 0;j<tds.size();j++){

                        String text = tds.get(j).text();
                        arr.add(text);
                    }

                    if (!"".equals(arr.get(0)) && !"".equals(arr.get(1))&& !"".equals(arr.get(2))){
                        //如果数据库最大时间比爬取到的时间小才进行插入操作
                        if(maxtime.compareTo(arr.get(0)) < 0){
                            TbFlowStateEntity flowStateEntity = new TbFlowStateEntity();
                            flowStateEntity.setTime(LocalDateTime.parse(arr.get(0), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))
                                    .setValue(arr.get(1))
                                    .setState(arr.get(2))
                                    .setEquipmentId(equipmentId);
                            list.add(flowStateEntity);
                        }
                    }
                }
                if(list.size() > 0){
                    // 批量插入流量数据
                    xiParse.spiderService.insertFlowState(list);
                }else {
                    System.out.println("-------------");
                }

            }

        }

    }
}
